package com.kb.design.mapper.postgreSQL;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kb.design.dto.CustomUserDTO;
import com.kb.design.entity.CustomUser;
import com.kb.design.entity.DesignDaily;
import com.kb.design.entity.DesignResponsiblePerson;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * (CustomUser)表数据库访问层
 *
 * @author makejava
 * @since 2023-08-22 10:56:59
 */
@Mapper
public interface DailyMapper extends BaseMapper<DesignDaily> {

    // 自定义方法，用于关联查询
//    @Select("SELECT o.*, u.username " +
//            "FROM orders o " +
//            "LEFT JOIN users u ON o.user_id = u.id " +
//            "WHERE o.id = #{orderId}")
//    List<DesignDaily> findOrderWithUser(@Param("orderId") Long orderId);

    @Select("SELECT dd.*,dp.project_name FROM design_daily dd LEFT JOIN design_project dp ON dd.project_id = dp.id")
    List<DesignDaily> findDailyWithUser();


    //    @Select("SELECT * FROM design_responsible_person WHERE project_id = #{projectId} AND node_status = #{nodeStatus}")
    @Select("SELECT * FROM design_responsible_person P WHERE P.project_id = #{projectId} ORDER BY P.create_time DESC LIMIT 1;")
    DesignResponsiblePerson findByProjectIdAndNodeStatus(Long projectId);

    /**
     * 查询空闲人数列表
     */
    @Select({
            "<script>",
            "SELECT d.*, p.dept_name FROM design_daily d",
            "LEFT JOIN design_user u ON d.create_by = u.code",
            "LEFT JOIN design_dept p ON p.dept_id = u.dept_id",
            "WHERE 1=1 AND d.daily = CURRENT_DATE",
            "<if test='createByName != null and createByName != \"\"'>",
            "AND d.create_by_name LIKE CONCAT('%', #{createByName}, '%')",
            "</if>",
            "<if test='freeSelect != null and freeSelect.size() > 0'>",
            "AND d.free_select IN",
            "<foreach collection='freeSelect' item='item' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</if>",
            "<if test='freeTime != null'>",
            "AND d.free_time = #{freeTime}",
            "</if>",
            "</script>"
    })
    List<DesignDaily> queryTodyFreeList(@Param("createByName") String createByName,
                                        @Param("freeSelect") List<Integer> freeSelect,
                                        @Param("freeTime") Integer freeTime);

}

